const app = getApp();
import { http }  from '../api'

//判断会员是否过期 data:  null 正常 1 未开通  2 会员到期 3 体验会员，次数用完 

export function  termOfValidityDate( cb,bool,message ) {

    http({ url: `back/user/check/termOfValidityDate`,data:{ id:app.globalData.userId },nomsg:true})
        .then(res => {
            
            // console.log("是否过期", res)

            if (res.code == 200) {

                if(!res.data){

                    cb && cb( true )
                }    
                else{

                    if( res.data !=1 ){

                        uni.showModal({
                            title: '温馨提示',
                            content: res.msg,
                            confirmText: "去续费",
                            cancelText: "取消",
                            showCancel:true,
                            success: function (res) {
                                if (res.confirm) {
                                    
                                    uni.navigateTo({
                                        url:'/pages/member/index'
                                    })
                                   
                                }
                                else{
        
                                    if(bool){
        
                                        uni.navigateBack()
                                    }
        
                                }
                            }
                        })
        

                    }

                    cb && cb( false )

                }
            } 

        })
        .catch((res)=>{

            if(app.globalData.userMianInfo.memberTypeId != 0 ){

                uni.showModal({
                    title: '温馨提示',
                    content: '您的会员已到期，是否立即续费',
                    confirmText: "去续费",
                    cancelText: "取消",
                    showCancel:true,
                    success: function (res) {
                        if (res.confirm) {
                            
                            uni.navigateTo({
                                url:'/pages/member/index'
                            })
                           
                        }
                        else{

                            if(bool){

                                uni.navigateBack()
                            }

                        }
                    }
                })

            }
            
            cb && cb( false )
            // console.log("是否过期1222", res)

        })

}


//4.超市支付订单
export function  GoPayOrder ( orderIds ,oldOrderMoney ,success,fail) {

    http({ url: `flash/supermarket/pay/wechat/smallAppletPay?loading`,data:{  orderIdList: orderIds ,openId:app.getOpenID(),productDesc:"甜城小二订单支付" }})
        .then(res => {


            // console.log("支付", res.data.totalMoney ,oldOrderMoney ,  res.data.totalMoney == oldOrderMoney )

            if (res.code == 200) {


                let resDate = res.data

                if( resDate.totalMoney != oldOrderMoney){

                    uni.showModal({
                        title: '温馨提示',
                        content: '配送费有变化,订单金额已重新计算,当前为 ￥'+ resDate.totalMoney +' ,继续支付?',
                        confirmText: "支付",
                        cancelText: "取消",
                        showCancel:true,
                        success: function (userRes) {
                            if (userRes.confirm) {
                                
                            
                                axiosOrder( resDate ,success,fail)
                            }
                            else{
    
                            }
                        }
                    })
    
                }
                else{

                      
                    axiosOrder( resDate ,success,fail)

                }          
            
            } else {
                uni.showToast({
                    title: res.message || '请求失败，请稍后再试!',
                    icon: 'none'
                });
            }
        })

}

//4.会员支付订单
export function  GoPayMemberOrder ( orderId ,success,fail) {

    http({ url: `flash/member/open/pay/wechat/smallAppletPay?loading`,data:{  orderId: orderId ,openId:app.getOpenID(),productDesc:"甜城小二订单支付" }})
        .then(res => {
            console.log("支付", res)

            if (res.code == 200) {
                
                axiosOrder(res.data ,success,fail)
            
            } else {
                uni.showToast({
                    title: res.message || '请求失败，请稍后再试!',
                    icon: 'none'
                });
            }
        })

}

//小程序支付-获取预支付ID

function axiosOrder (obj ,success,fail ) {

    // let dataObj = res.dataObj
    wx.requestPayment({
        timeStamp: obj.timeStamp,
        nonceStr: obj.nonceStr,
        package: obj.package,
        signType: obj.signType,
        paySign: obj.paySign,
        success: res => {

            console.log('pay success', res);

            uni.showToast({
                title: '支付成功',
                icon: 'success',
                success: () => {
                   
                    success && success()
                }
            });
        },
        fail: err => {
            uni.showToast({
                title: '支付失败',
                icon: 'none'
            });

            fail && fail()

        }
    });
}
